package com.example.pfe_copie_finale;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListAdapter;
import android.widget.SimpleAdapter;
import android.widget.Toast;

public class NewClientActivity extends Activity {
	// Progress Dialog
    private ProgressDialog pDialog;

    ArrayList<HashMap<String, String>> clientList;
    jsonParser jsonParser = new jsonParser();
    EditText inputRaisonsociale;
    EditText inputTelephone;
    EditText inputEmail;
    EditText inputFax;
    EditText inputObservation;
 
    // url to create new product
    private static String url_create_client = "http://10.0.2.2/addClient";
    
 // JSON Node names
    private static final String TAG_SUCCESS = "success";
 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ajout_client);
 
        // Edit Text
        inputRaisonsociale = (EditText) findViewById(R.id.raisonsociale);
        inputTelephone = (EditText) findViewById(R.id.telephone);
        inputFax = (EditText) findViewById(R.id.fax);
        inputObservation = (EditText) findViewById(R.id.observation);
        inputEmail = (EditText) findViewById(R.id.email);
     // Create button
        Button enregistrer = (Button) findViewById(R.id.enregistrer);
     // button click event
        enregistrer.setOnClickListener(new View.OnClickListener() {
 
            @Override
            public void onClick(View view) {

                // creating new product in background thread
                new CreateNewClient().execute();
            }
        });

}
   
    
    
    
    
    
    public class CreateNewClient extends AsyncTask<String, String, String> {
    	
        @Override
        protected void onPreExecute() {
        	
            super.onPreExecute();
            ProgressDialog pDialog = new ProgressDialog(NewClientActivity.this);
            pDialog.setMessage("Creating Client..");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(true);
            pDialog.show();
        }
    	


    	@Override
    	protected String doInBackground(String... arg0) {
    		
    		String raisonsociale = inputRaisonsociale.getText().toString();
            String email = inputEmail.getText().toString();
            String telephone = inputTelephone.getText().toString();
            String fax = inputFax.getText().toString();
            String observation = inputObservation.getText().toString();
         // Building Parameters

            Log.d("Create Response", "xxxxxxxxxxxxxxx");
            List<NameValuePair> params = new ArrayList<NameValuePair>();
            params.add(new BasicNameValuePair("raisonsociale", raisonsociale));
            params.add(new BasicNameValuePair("email", email));
            params.add(new BasicNameValuePair("telephone", telephone));
            params.add(new BasicNameValuePair("fax", fax));
            params.add(new BasicNameValuePair("observation", observation));

           
         // getting JSON Object
            // Note that create product url accepts POST method
            JSONObject json = jsonParser.makeHttpRequest(url_create_client,"POST", params);

            // check log cat fro response
            Log.d("Create Response", json.toString());
            
            // check for success tag
            try {
                int success = json.getInt(TAG_SUCCESS);
                Log.d("Create Response","aa   "+success);
                if (success == 1) {
                    Intent i = new Intent(getApplicationContext(), ListeClient.class);
                    startActivity(i);
                    Log.i("finish", "will finish");
                    // closing this screen
                    finish();
                    Log.i("finish", "finished !!!!!!!!");
                    }
            } catch (JSONException e) {
                e.printStackTrace();
            }

            return null;
        }

    	/**
         * After completing background task Dismiss the progress dialog
         * **/
//        protected void onPostExecute(String file_url) {
//            // dismiss the dialog once done
//        	Log.i("pdialog","will dismiss");
//              //pDialog.dismiss();
//              dismissDialog(1);
//              Log.i("pdialog","dismissed !!!!");  
//        }
    }
}







